# Contributor: Runar Tenfjord < runar dot tenfjord at gmail dot com >
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cython
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-cython" "${MINGW_PACKAGE_PREFIX}-cython2")
pkgver=0.29.14
pkgrel=1
pkgdesc="C-Extensions for Python (mingw-w64)"
arch=('any')
url="https://cython.org/"
license=('APACHE')
makedepends=(
            "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
            "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
            "${MINGW_PACKAGE_PREFIX}-gcc")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/cython/cython/archive/${pkgver}.tar.gz")
sha256sums=('6a3370fda27c020c172805c6257f222624bdd5f73653bb93f4af4e9f282d17f7')

prepare() {
  cd "${srcdir}"
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH}| true
    cp -r "${_realname}-${pkgver}" "python${pver}-build-${CARCH}"
    sed -e "s|\/usr\/bin\/python|${MINGW_PREFIX}\/bin\/python${pver}|" -i python${pver}-build-${CARCH}/setup.py
  done
}

package_cython() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  DISTUTILS_DEBUG=1 \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
                              --root="${pkgdir}" --optimize=1

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_cython2() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
  cd "${srcdir}/python2-build-${CARCH}"
  DISTUTILS_DEBUG=1 \
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
                              --root="${pkgdir}" --optimize=1

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}.exe
  if [ -f ${pkgdir}${MINGW_PREFIX}/bin/cygdb.exe.manifest ]; then
    mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}.exe.manifest
    mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}.exe.manifest
    mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}.exe.manifest
  fi

  mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}-script.py
}

package_mingw-w64-i686-cython() {
  install=${_realname}-${CARCH}.install
  package_cython
}

package_mingw-w64-i686-cython2() {
  install=${_realname}2-${CARCH}.install
  package_cython2
}

package_mingw-w64-x86_64-cython() {
  install=${_realname}-${CARCH}.install
  package_cython
}

package_mingw-w64-x86_64-cython2() {
  install=${_realname}2-${CARCH}.install
  package_cython2
}
